/* Plug in for jQuery. It demonstrates how to get access to the
 * jQuery namespace and extend it for vendor specific namespace.
 * Example further shows adding one static class and a class
 * into the vendor specific namespace.
 */
(function($) {
 	$.opentext = {};
	$.opentext.tools = {};
	$.opentext.tools.zoom = (function() {
		var cfg = {};
		return {
			set: function(a) {
				console.log( 'opentext.tools.zoom.set static fucntion called: ' + a );
				cfg.zoomFactor = a;
			},
			get: function() {
				console.log( 'opentext.tools.zoom.get static function called and returning: ' + cfg.zoomFactor );
				return cfg.zoomFactor;
			}
		};
	}());

	$.opentext.tools.sum = function(a) {
		var total = 0;
		$.each(a, function(index, value) {
			value = $.trim(value);
			value = parseFloat(value) || 0;
			total += value;
		});
		return total;
	};

	$.opentext.star = function(pars) {
		var data = this;
		data.cfg = {};
		for(i in pars)
			if(pars[i] !== undefined)
				data.cfg[i] = pars[i];
		return {
			data: this.cfg,
			show: function(par) {
				this.data.dia = par;
				console.log('opentext.star.show volana pro cislo = ' + this.data.dia );
				console.log('opentext.star.show name:' + this.data.name );
			},
			setName: function(n) {
				this.data.name = n;
			},
			getName: function() {
				return this.data.name;
			},
			reset: function() {
				this.data.dia = 0;
				this.data.name = null;
				console.log('opentext.star.reset');
			}
		}
	};

	/* Widget works if the jQuery UI classes are included. The style
	 * for the tooltip ie defined in the calling application as well,
	 * named 'opentext-tooltip-text'. The tooltip can be activated
	 * for eny element. The element must have attribute 'data-tooltip-text'.
	 * jQuery UI core and widget process the attribute and creates data
	 * container in 'context.dataset.tooltipText'.
	 */
	$.widget('opentext.tooltip', {
		_create: function() {
			this._tooltipDiv = $('<div></div>').addClass('opentext-tooltip-text ' + 'ui-widget ui-state-highlight ui-corner-all').hide().appendTo('body');
      	this.element.addClass('opentext-tooltip-trigger').bind('mouseenter.opentext-tooltip', $.proxy(this._open, this)).bind('mouseleave.opentext-tooltip', $.proxy(this._close, this));
    	},

		_open: function() {
			var elementOffset = this.element.offset();
			this._tooltipDiv.css(
				{
        			left: elementOffset.left + 12,
        			top: elementOffset.top + this.element.height() + 4
      		}).text(this.element.context.dataset.tooltipText);
      	this._tooltipDiv.show();
		},

		_close: function() {
			this._tooltipDiv.hide();
		}
	});

 })(jQuery);
